define(function(require, exports, module) {
    'use strict';
    
    var rk = require('rk');
    
    
    require('../su/accordion/oaTabCtrl');
    
    $.widget('rk.groupContentCtrl', $.rk.oaContentCtrl, {
        _create: function() {
            var me = this;
            var elem = me.element;
            var opt = me.options;
            var temp = require('oa/tmpl/group/content/tpl_group_content.tpl');
            var html = rk.templateText(temp);
            elem.html(html);
            elem.find("[act='group_content_follow']").beTip();
            elem.find("[act='group_content_tab']").tabs({
                active: 0,
                activate: function(e, ui) {
                    $.proxy(me.showFroupContentTabs(e, ui), me);
                }
            });
            var divs = $("#group_content_index_tab");
            me.loaddata(opt.type, {}, divs);
            me._listeners();
        },
        _listeners: function() {
            var me = this;
            var elem = me.element;
            var opt = me.options;
            me._group_content_listeners();
        },
        _group_content_listeners: function() {
            var me = this;
            var elem = me.element;
            var opt = me.options;
            elem.on("click", "[act='group_content_set_button']", function() {
                require.async(["./content/groupSetCtrl.js"], function() {
                    rk.dialog({
                        title: rk.i18n('GROUP_SET_GROUP'),
                        success: function(contentElem) {
                            contentElem.groupSetCtrl({
                                gid: opt.gid,
                                type: "group",
                                loadParam: opt.loadParam
                            }); //初始化弹框内容的Ctrl
                        },
                        footer: ['cancel', {
                            act: 'ok',
                            visible: true,
                            text: rk.i18n('SAVE')
                        }]
                    });
                });
            });
            elem.on("click", "[act='group_content_follow']", function() {
                var button = $(this);
                var isFollow = button.attr("isFollow");
                if (button.hasClass('disable')) return;
                button.addClass('disable');
                if (isFollow == "1") {
                    rk.confirm(rk.i18n('COLLEAGUE_CONFIRM_UNFOLLOW'), function() {
                        rk.post("/json/oa_group/member-delfollow.action", {
                            gid: opt.gid
                        }, function(json) {
                            if (json.status === 0) {
                                button.addClass("no_follow");
                                button.removeClass("has_follow");
                                button.attr("isFollow", "0");
                                button.attr("title", rk.i18n('COLLEAGUE_ADD_FOLLOW'));
                                rk.noticeSuccess(rk.i18n('COLLEAGUE_OPERATE_SUCCESS'));
                            } else if (json.status === 217002) {
                                rk.noticeError(rk.i18n('COLLEAGUE_GROUP_BELONG_ERROR'));
                            } else {
                                rk.noticeError(rk.i18n('COLLEAGUE_SYSTEM_ERROR'));
                            }
                            button.removeClass('disable');
                        });
                    }, function() {
                        button.removeClass('disable');
                    });
                } else if (isFollow == "0") {
                    rk.post("/json/oa_group/member-follow.action", {
                        gid: opt.gid
                    }, function(json) {
                        if (json.status === 0) {
                            button.removeClass("no_follow");
                            button.addClass("has_follow");
                            button.attr("isFollow", "1");
                            button.attr("title", rk.i18n('GROUP_UNFOLLOW'));
                            rk.noticeSuccess(rk.i18n('COLLEAGUE_OPERATE_SUCCESS'));
                        } else if (json.status === 217002) {
                            rk.noticeError(rk.i18n('COLLEAGUE_GROUP_BELONG_ERROR'));
                        } else {
                            rk.noticeError(rk.i18n('COLLEAGUE_SYSTEM_ERROR'));
                        }
                        button.removeClass('disable');
                    });
                }
            });
            elem.on('mouseover', "[act='group_one_join_out']", function(){
                var button = $(this);
                var ismember = button.attr("ismember");
                if(ismember == 1) {
                    button.html(rk.i18n('GROUP_SIGN_OUT'));
                }
            });
            elem.on('mouseout', "[act='group_one_join_out']", function(){
                var button = $(this);
                var ismember = button.attr("ismember");
                if(ismember == 1) {
                    button.html(rk.i18n('GROUP_HAVE_JOIN'));
                }
            });
            elem.on("click", "[act='group_one_join_out']", function() {
                var button = $(this);
                var isMember = button.attr("isMember");
                //var uid = opt.uid;
                var uid = SESSION.user.id;
                var gid = opt.gid;
                if (uid && gid) {
                    if (button.hasClass('disable')) return;
                    button.addClass('disable');
                    if (isMember == "1") {
                        rk.confirm(rk.i18n('GROUP_EXIT_CONFIRM'), function() {
                            rk.post("/json/oa_group/member-quit.action", {
                                uid: uid,
                                gid: gid
                            }, function(json) {
                                if (json.status === 0) {
                                    button.attr("isMember", "0");
                                    button.removeClass("group_button_in");
                                    button.addClass("group_button_join");
                                    button.html(rk.i18n('GROUP_JOIN'));
                                    button.attr("title",rk.i18n('GROUP_JOIN'));
                                    elem.find("[act='group_content_follow_div']").hide();
                                    elem.find("[act='group_content_follow']").addClass("no_follow");
                                    elem.find("[act='group_content_follow']").removeClass("has_follow");
                                    elem.find("[act='group_content_follow']").attr("isFollow", "0");
                                    elem.find("[act='group_content_follow']").attr("title", rk.i18n('COLLEAGUE_ADD_FOLLOW'));
                                    rk.noticeSuccess(rk.i18n('COLLEAGUE_OPERATE_SUCCESS'));
                                } else {
                                    rk.noticeError(rk.i18n('COLLEAGUE_SYSTEM_ERROR'));
                                }
                                button.removeClass('disable');
                            });
                        }, function() {
                            button.removeClass('disable'); //取消
                        });

                    } else if (isMember == "0") {
                        rk.post("/json/oa_group/member-join.action", {
                            uid: uid,
                            gid: gid
                        }, function(json) {
                            if (json.status === 0) {
                                button.attr("isMember", "1");
                                button.removeClass("group_button_join");
                                button.addClass("group_button_in");
                                button.html(rk.i18n('GROUP_HAVE_JOIN'));
                                button.attr("title",rk.i18n('GROUP_SIGN_OUT'));
                                elem.find("[act='group_content_follow_div']").show();
                                elem.find("[act='group_content_follow']").removeClass("no_follow");
                                elem.find("[act='group_content_follow']").addClass("has_follow");
                                elem.find("[act='group_content_follow']").attr("isFollow", "1");
                                elem.find("[act='group_content_follow']").attr("title", rk.i18n('GROUP_UNFOLLOW'));
                                rk.noticeSuccess(rk.i18n('COLLEAGUE_OPERATE_SUCCESS'));
                            } else {
                                rk.noticeError(rk.i18n('COLLEAGUE_SYSTEM_ERROR'));
                            }
                            button.removeClass('disable');
                        });
                    }
                }
            });
            elem.on("click", "[act='group_delete_group']", function() {
                rk.confirm(rk.i18n('GROUP_DELETE_CONFIRM'), {
                    okText: rk.i18n('DELETE')
                }, function() {
                    var gid = opt.gid;
                    rk.post("/json/oa_group/delete-group.action", {
                        gid: gid
                    }, function(json) {
                        if (json.status === 0) {
                            me.renewPanel();
                            me.closeMe();
                            rk.noticeSuccess(rk.i18n('COLLEAGUE_OPERATE_SUCCESS'));
                        } else if (json.status === 217001) {
                            rk.noticeError(rk.i18n('GROUP_NO_PERMISSION'));
                        } else {
                            rk.noticeError(rk.i18n('COLLEAGUE_SYSTEM_ERROR'));
                        }
                    });
                }, function() {

                });
            });
            elem.on("click", "[act='group_add_member']", function() {
                require.async(["./content/groupAddMember"], function() {
                    rk.dialog({
                        title: rk.i18n('GROUP_ADD_MEMBER'),
                        height: "200",
                        success: function(contentElem) {
                            contentElem.groupAddMember({
                                loaddataType: me.loaddataType,
                                gid: opt.gid
                            });
                        },
                        footer: ['ok']
                    });
                });

            });
            me._group_content_index_listener();
        },
        _group_content_index_listener: function() {
            var me = this;
            var elem = me.element;
        },
        showFroupContentTabs: function(e, ui) {
            var me = this;
            var elem = me.element;
            if (ui) {
                var divs = ui.newPanel;
                me.loaddata(divs.attr("loadDataType"), {}, divs);
            }
        },
        _init: function() {
            var me = this;
            var elem = me.element;
            elem.hide();
        },
        loaddata: function(type, param, divs) {
            var me = this;
            var elem = me.element;
            if (!param) {
                param = {
                    pageNo: 1,
                    pageSize: 20
                };
            }
            if (type && me.loadDataJson[type]) {
                $.proxy(me.loadDataJson[type].call(this, param, divs), me);
            }
        },
        renewPanel: function() {
            var me = this;
            var elem = me.element;
            var opt = me.options;
            var panel = rk.oa.getPanel();
            if (panel) {
                require.async(["./groupPanelCtrl"], function() {
                    if (opt.loadParam) {
                        panel.groupPanelCtrl("loaddata", opt.loadParam.type, opt.loadParam.param);
                    }
                });
            }
        },
        _permission: 0,
        loadDataJson: {
            "group": function(param, divs) {
                var me = this;
                var elem = me.element;
                var opt = me.options;
                divs.beLoading();
                rk.post("/json/oa_group/group-detail.action", {
                    gid: opt.gid
                }, function(json) {
                    if (json.status == 0) {
                        if (json.canAccess) {
                            elem.show();
                            if (json.group) {
                                var temp = require('oa/tmpl/group/content/tpl_group_content_index.tpl');
                                var html = rk.templateText(temp, json.group);
                                elem.find("[act='group_content_name']").text(json.group.name);
                                if (divs) {
                                    divs.html(html);
                                    require.async(["core/apps/feed/feed"], function() {
                                        var arCallbacks = [];
                                        var height;
                                        if(elem.parent().parent() && elem.parent().parent().parent().attr("act") == "oacontent"){
                                            height = elem.parent().parent().height();
                                        }else{
                                            height = $("#rk-panel").height() - 50;
                                        }
                                        elem.height(height);
                                        elem.beScroll({
                                            whileScrolling: function() {
                                                var scrollTop = Math.abs(parseInt(elem.find('.mCSB_container').css('top'), 10));
                                                if (scrollTop > elem.closest('.mCustomScrollbar').height()) {
                                                    divs.find('a.js-gototop').show();
                                                } else {
                                                    divs.find('a.js-gototop').hide();
                                                }
                                            },
                                            onTotalScroll: function() {
                                                for (var i = 0, len = arCallbacks.length; i < len; i++) {
                                                    var fn = arCallbacks[i];
                                                    if ($.isFunction(fn)) {
                                                        fn();
                                                    }
                                                }
                                            },
                                            onTotalScrollOffset: 120,
                                            alwaysTriggerOffsets: true
                                        });
                                        divs.find("[act='group_group_message']").feed2015({
                                            scrollRegister: function(fn) {
                                                arCallbacks.push(fn);
                                            },
                                            tabs: [{
                                                tabname: 'none',
                                                prependnew: 'true',
                                                url: '/json/sns_feed/groupFeeds.action?gid=' + opt.gid,
                                                appendmore: 'true'
                                            }],
                                            hasEditor: true,
                                            groupId : opt.gid
                                        });
                                    });
                                }
                                me._permission = json.group.permission;
                                if (json.group.permission != 0) {
                                    elem.find("[act='group_is_private']").show();
                                } else {
                                    elem.find("[act='group_is_private']").hide();
                                }

                                if (json.group.isGroupAdmin) { //colleague_depart_set_div
                                    elem.find("[setButton='group_content_set']").show();
                                } else {
                                    elem.find("[setButton='group_content_set']").hide();
                                }

                                if (json.group.isGroupMember) {
                                    elem.find("[act='group_content_follow_div']").show();
                                } else {
                                    elem.find("[act='group_content_follow_div']").hide();
                                }

                                if (json.group.permission != 0) {
                                    elem.find("[act='group_one_join_out']").hide();
                                } else {
                                    if (json.group.isGroupAdmin) {
                                        elem.find("[act='group_one_join_out']").hide();
                                    } else {
                                        var groupJoinOut = elem.find("[act='group_one_join_out']");
                                        groupJoinOut.show();
                                        if (json.group.isGroupMember) {
                                            groupJoinOut.attr("isMember", "1");
                                        } else {
                                            groupJoinOut.attr("isMember", "0");
                                            groupJoinOut.removeClass("group_button_in");
                                            groupJoinOut.addClass("group_button_join");
                                            groupJoinOut.html(rk.i18n('GROUP_JOIN'));
                                            groupJoinOut.attr("title",rk.i18n('GROUP_JOIN'));
                                        }
                                    }
                                }

                                if (json.group.isGroupFollow) {
                                    elem.find("[act='group_content_follow']").removeClass("no_follow");
                                    elem.find("[act='group_content_follow']").addClass("has_follow");
                                    elem.find("[act='group_content_follow']").attr("isFollow", "1");
                                    elem.find("[act='group_content_follow']").attr("title", rk.i18n('GROUP_UNFOLLOW'));
                                } else {
                                    elem.find("[act='group_content_follow']").addClass("no_follow");
                                    elem.find("[act='group_content_follow']").removeClass("has_follow");
                                    elem.find("[act='group_content_follow']").attr("isFollow", "0");
                                    elem.find("[act='group_content_follow']").attr("title", rk.i18n('COLLEAGUE_ADD_FOLLOW'));
                                }
                            } else {
                                var temp = require("oa/tmpl/group/content/tpl_no_group.tpl");
                                var html = rk.templateText(temp, {
                                    text: rk.i18n('GROUP_DELETED')
                                });
                                elem.html(html);
                                elem.show();
                            }
                        } else {
                            var temp = require("oa/tmpl/group/content/tpl_no_group.tpl");
                            var html = rk.templateText(temp, {
                                text: rk.i18n('COLLEAGUE_NO_AUTHORITY_ERROR')
                            });
                            elem.html(html);
                            elem.show();
                        }
                    } else if(json.status == 217003){
                        var temp = require("oa/tmpl/group/content/tpl_no_group.tpl");
                        var html = rk.templateText(temp, {
                            text: rk.i18n('GROUP_DELETED')
                        });
                        elem.html(html);
                        elem.show();
                    } else{
                        //rk.noticeError(rk.i18n('COLLEAGUE_SYSTEM_ERROR'));
                        var temp = require("oa/tmpl/group/content/tpl_no_group.tpl");
                        var html = rk.templateText(temp, {
                            text: rk.i18n('COLLEAGUE_CANT_VISIT_ERROR')
                        });
                        elem.html(html);
                        elem.show();
                    }
                });
            },
            "group_member": function(param, divs) {
                divs.beLoading();
                var me = this;
                var elem = me.element;
                var opt = me.options;
                param.gid = opt.gid;
                require.async(["./content/groupContentMemberTabCtrl.js"], function() {
                    divs.groupContentMemberTabCtrl({
                        gid: opt.gid
                    });
                });
            },
            "group_file": function(param, divs) {
                var me = this;
                var elem = me.element;
                var opt = me.options;
                require.async(["../group/content/groupContentFileCtrl.js"], function() {
                    divs.groupContentFileCtrl({
                        gid: opt.gid,
                        type: "group",
                        permission: me._permission
                    });
                });
            }
        }
    });
    return {
        init: function(rkContent, id) {
            rkContent.groupContentCtrl({
                gid: id,
                type: "group"
            });
        }
    }
});
